$(document).ready(function() {
	$('#checkAll').click(function(){
		var status = $(this).attr('checked');
		$('input[name="PRODUCT_ID"]').attr('checked', status);
	});
	$Base.isLoginManage(getProductList);
});

function getProductList(){
	var name = $.trim($('#search_name').val());
	var status = $('#search_status').val();
	var url = '/json?action=MANAGE_QUERY_PRODUCT_PAGER_ACTION&PRODUCT_NAME=' + name + '&STATUS=' + status;
	var current = $('#pageId').val();
	$UI.changePage(url,10,'T_PRODUCT','#list',printRes,'#page','<tr><td colspan="8"><div style="text-align:center">暂无数据</div></td></tr>',current,null,'#pageId');
}

function printRes(obj,result,i,n,total,current){
	var trStr = '';
	trStr += '<tr '+ (i%2 == 0 ? "" : "class=\'alt\'") +'>';
	trStr += '<td><input type="checkbox" name="ID" value="'+obj.PRODUCT_ID+'"/></td>';
	trStr += '<td>'+obj.PRODUCT_NO+'</td>';
	trStr += '<td><a href="../../product/file.html?id='+obj.PRODUCT_ID+'" target="_blank"><img src="../../upload/product/'+obj.PRODUCT_NO+'.jpg" width="50" height="50" align="middle" /></a>' +
			'<span style="padding:5px;"><a href="../../product/file.html?id='+obj.PRODUCT_ID+'" target="_blank">' + obj.PRODUCT_NAME + '</a></span></td>';
	trStr += '<td>'+obj.PRODUCT_BARCODE+'</td>';
	trStr += '<td>￥'+obj.MARKET_PRICE+'</td>';
	trStr += '<td>￥'+obj.MEMBER_PRICE+'</td>';
	trStr += '<td>'+(obj.PRODUCT_STATUS == 1 ? '上架' : '下架')+'</td>';
	trStr += '<td>';
	trStr += '<a href="#">编辑</a>';
	
	trStr += ' <a href="javascript:$Base.isLoginManage(updateStatus,{id:'+obj.PRODUCT_ID+',status:'+obj.PRODUCT_STATUS+',name:\''+obj.PRODUCT_NAME+'\'})">'+(obj.PRODUCT_STATUS == 1 ? '下架' : '上架')+'</a>';
	trStr += ' <a href="javascript:$Base.isLoginManage(deleteProduct,{id:'+obj.PRODUCT_ID+',name:\''+obj.PRODUCT_NAME+'\'})">删除</a>';
	trStr +='</td>';
	trStr +='</tr>';
	$(result).append(trStr);
}

var msg_second = 2000;
function edit(id){
	$.zxxbox.ajax("update.html", {s:Math.random()}, {
		title:"修改产品",
		drag:true, 
		protect:false, 
		ajaxCallback: function(){
			$('#ORDER_ID').val(id);
			var url = '/json?action=MANAGE_GET_ORDER_BY_ID_ACTION';
			$.post(url,{ORDER_ID:id},function(data){
				if(data.success){
					var obj = data.root.T_ORDER.rs[0];
					$('#plist').html(html);
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	});
}

function updateStatus(param){
	var status = param.status;
	var oper = "";
	if(status == 1){
		status = 0;
		oper = "下架";
	}
	else if(status == 0){
		status = 1;
		oper = "上架";
	}
	var btnList = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_UPDATE_PRODUCT_STATUS_BY_ID_ACTION&PRODUCT_ID='+param.id +'&STATUS='+status;
			$.post(url, {}, function(data){
				if(data.success){
					getProductList();
					$.zxxbox.msg(oper+"成功！", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			}, 'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		css: 'cancel_btn',
		click: function(){
			$.zxxbox.hide();
		}
	}];
	$.zxxbox.ask("您确定要"+oper+"【" + param.name + "】吗？", btnList, {title: "操作确认"});
}

function deleteProduct(param){
	var btnList = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_DELETE_PRODUCT_BY_ID_ACTION&PRODUCT_ID='+param.id;
			$.post(url, {}, function(data){
				if(data.success){
					getProductList();
					$.zxxbox.msg("删除成功！", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			}, 'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		css: 'cancel_btn',
		click: function(){
			$.zxxbox.hide();
		}
	}];
	$.zxxbox.ask("您确定要删除【" + param.name + "】吗？", btnList, {title: "操作确认"});
}


